package com.zb.kc.ucenter.controller;


import com.zb.kc.commonutils.JwtUtils;
import com.zb.kc.commonutils.R;
import com.zb.kc.commonutils.ordervo.UcenterMemberOrder;
import com.zb.kc.servicebase.exceptionhandler.KgcException;
import com.zb.kc.ucenter.entity.UcenterMember;
import com.zb.kc.ucenter.entity.vo.LoginVo;
import com.zb.kc.ucenter.entity.vo.RegisterVo;
import com.zb.kc.ucenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author wanggen
 * @since 2020-04-20
 */
@RestController
@RequestMapping("/eduucenter/member")
@Slf4j
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    // 登录
    @PostMapping("login")
    public R loginUser(@RequestBody LoginVo member) {
        String token = memberService.login(member);
        return R.ok().data(token);
    }

    // 注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("getMemberInfo")
    public R getLoginInfo(HttpServletRequest request) {
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember member = memberService.getById(memberId);
            return R.ok().data(member);
        } catch (Exception e) {
            e.printStackTrace();
            throw new KgcException(20001, "error");
        }
    }

    @ApiOperation(value = "根据用户id返回用户昵称、头像")
    @GetMapping("getUserLessInfo/{id}")
    public Map<String, Object> getUserLessInfo(@PathVariable("id") String id) {
        Map<String, Object> map = memberService.getUserLessInfo(id);
        return map;
    }

    @ApiOperation(value = "根据用户id获取用户信息")
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable("id") String id) {
        UcenterMember member = memberService.getById(id);
        UcenterMemberOrder memberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, memberOrder);
        return memberOrder;
    }

    // 查询某一天的注册人数
    @ApiOperation(value = "查询某一天的注册人数")
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data(count);
    }

}
